package com.itat;

import com.alibaba.fastjson.JSON;
import com.itat.client.NettyClient;
import com.itat.client.ReconnectClient;
import com.itat.protocol.RpcRequest;
import io.netty.channel.Channel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.UUID;


/**
 * client Booter
 * @author baixuefeng
 * @date 2021-12-29
 */
public class NettyClientStarter {
    private static final Log log = LogFactory.getLog(NettyClientStarter.class);


    //默认端口
    private static final int defaultPort=56709;

    //单机连接板
    private static void onlyClientMessage() throws Exception {
        NettyClient client = new NettyClient("127.0.0.1", defaultPort);
        //启动client服务
        client.start();

        Channel channel = client.getChannel();
        //消息体
        RpcRequest request = new RpcRequest();
        request.setId(UUID.randomUUID().toString());
        request.setData("client.message");
        log.info("客户端发送:"+ JSON.toJSONString(request));
        //channel对象可保存在map中，供其它地方发送消息
        channel.writeAndFlush(request);
    }

    //重试连接板
    private static void retryClientMessage() throws Exception {
        //重试连接板
        ReconnectClient client = new ReconnectClient("127.0.0.1", defaultPort);
        //启动client服务
        client.start();

        //消息体
        RpcRequest request = new RpcRequest();
        request.setId(UUID.randomUUID().toString());
        request.setData("client: How are you.");
        client.sendData(request);//主动发送
        Thread.sleep(10000L);
        ReconnectClient.addQuene(request);//retry发送
//        client.getChannel().writeAndFlush(request);//主动发送
    }

    public static void main(String[] args) throws Exception {

//        onlyClient();

        retryClientMessage();

    }
}
